﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using KingNET.Controls;
using KingNET.Engine;

namespace KingNET.Forms
{
    public partial class frmBoard : Form
    {

        #region MinimumSize

        private readonly Size minimumFormSize = new Size(800, 700);

        /// <summary>
        /// Minimum form size
        /// </summary>
        public override Size MinimumSize
        {
            get
            {
                return minimumFormSize;
            }
            set
            {
                if ((value.Height != minimumFormSize.Height && value.Width != minimumFormSize.Width) || value.IsEmpty)
                    return;

                base.MinimumSize = value;
            }
        }
        #endregion

        // Game
        private readonly Game game;

        // Players
        private readonly Player playerNorth;
        private readonly Player playerWest;
        private readonly Player playerEast;
        private readonly Player playerSouth;

        // Player Name Font Properties
        private readonly Font font = new Font(SystemFonts.DefaultFont.FontFamily.Name, 10, FontStyle.Bold);
        private readonly SolidBrush brush = new SolidBrush(Color.White);

        // Büyüme Oranı
        float scaleWidth = 1F;
        float scaleHeight = 1F;


        #region Ctor

        public frmBoard()
        {
            InitializeComponent();

            //

            this.Size = this.MinimumSize;
            this.StartPosition = FormStartPosition.CenterScreen;
            //

            this.cardContainer.Panel1.BackColor = Color.DarkGreen; // ProfessionalColors.ToolStripGradientBegin;
            this.cardContainer.Panel1.Resize += new EventHandler(Panel1_Resize);
            this.cardContainer.Panel1.Paint += new PaintEventHandler(Panel1_Paint);

            #region Game

            game = new Game();
            playerEast = new Player();
            playerEast.Name = "Player East";

            playerWest = new Player();
            playerWest.Name = "Player West";

            playerNorth = new Player();
            playerNorth.Name = "Player North";

            playerSouth = new Player();
            playerSouth.Name = "Player South";

            game.Players.Add(playerEast);
            game.Players.Add(playerNorth);
            game.Players.Add(playerSouth);
            game.Players.Add(playerWest);

            game.DealCards();

            foreach (Card card in playerSouth.Cards)
            {
                //card.ShowCard = true;
                this.cardContainer.Panel1.Controls.Add(card);
            }


            foreach (Card card in playerNorth.Cards)
            {
                //card.ShowCard = false;
                this.cardContainer.Panel1.Controls.Add(card);
            }

            foreach (Card card in playerWest.Cards)
            {
                //card.ShowCard = false;
                this.cardContainer.Panel1.Controls.Add(card);
            }


            foreach (Card card in playerEast.Cards)
            {
                //card.ShowCard = false;
                this.cardContainer.Panel1.Controls.Add(card);
            }

            TileCards();

            foreach (Card card in playerSouth.Cards)
                System.Diagnostics.Debug.WriteLine(card.Suit + " -> " + card.CardValue);

            #endregion

        }

        #endregion

        #region TileCards
        /// <summary>
        /// Arrange cards on board
        /// </summary>
        void TileCards()
        {

            // South

            int allCardsWidth = (playerSouth.Cards.Count - 1) * (int)((float)Constants.CARD_DISPLAY_WIDTH * scaleWidth);
            int left = (cardContainer.Panel1.Width - allCardsWidth - playerSouth.Cards[0].Width) / 2;

            foreach (Card card in playerSouth.Cards)
            {
                card.Left = left;
                left += (int)((float)Constants.CARD_DISPLAY_WIDTH * scaleWidth);
                card.Top = cardContainer.Panel1.Height - card.Height - Constants.CARD_DISTANCE;
                card.SendToBack();
            }


            // North

            allCardsWidth = (playerNorth.Cards.Count - 1) * (int)((float)Constants.CARD_DISPLAY_WIDTH * scaleWidth);
            left = (cardContainer.Panel1.Width - allCardsWidth - playerNorth.Cards[0].Width) / 2;

            foreach (Card card in playerNorth.Cards)
            {
                card.Left = left;
                left += (int)((float)Constants.CARD_DISPLAY_WIDTH * scaleWidth);
                card.Top = cardContainer.Panel1.Top + Constants.CARD_DISTANCE;
                card.SendToBack();
            }

            // West

            int allCardsHeght = playerWest.Cards[0].Height + (playerWest.Cards.Count - 1) * (int)((float)Constants.CARD_DISPLAY_HEIGHT * scaleHeight);
            int top = (cardContainer.Height - allCardsHeght) / 2;

            foreach (Card card in playerWest.Cards)
            {
                card.Top = top;
                top += (int)((float)Constants.CARD_DISPLAY_HEIGHT * scaleHeight);
                card.Left = Constants.CARD_DISTANCE;
                card.SendToBack();
            }

            // East

            allCardsHeght = playerEast.Cards[0].Height + (playerEast.Cards.Count - 1) * (int)((float)Constants.CARD_DISPLAY_HEIGHT * scaleHeight);
            top = (cardContainer.Height - allCardsHeght) / 2;

            foreach (Card card in playerEast.Cards)
            {
                card.Top = top;
                top += (int)((float)Constants.CARD_DISPLAY_HEIGHT * scaleHeight);
                card.Left = cardContainer.Panel1.Width - playerEast.Cards[0].Width - Constants.CARD_DISTANCE;
                card.SendToBack();
            }


        }
        #endregion


        void Panel1_Resize(object sender, EventArgs e)
        {
            this.TileCards();
        }

        void Panel1_Paint(object sender, PaintEventArgs e)
        {

            int leftPos = (cardContainer.Panel1.Width - (int)e.Graphics.MeasureString(playerNorth.Name.Trim(), font).Width) / 2;
            int rightPos = playerNorth.Cards[0].Bottom + Constants.PLAYER_NAME_DISTANCE;

            // Horizontal player names
            e.Graphics.DrawString(playerNorth.Name.Trim(), font, brush, leftPos, rightPos);

            //
            SizeF sf = e.Graphics.MeasureString(playerSouth.Name.Trim(), font);
            rightPos = playerSouth.Cards[0].Top - Constants.PLAYER_NAME_DISTANCE - (int)sf.Height;
            e.Graphics.DrawString(playerSouth.Name.Trim(), font, brush, leftPos, rightPos);

            // Vertical player names

            e.Graphics.TranslateTransform(0, this.cardContainer.Panel1.ClientSize.Height);
            e.Graphics.RotateTransform(270F);

            sf = e.Graphics.MeasureString(playerWest.Name.Trim(), font);
            leftPos = cardContainer.Height - (int)(cardContainer.Panel1.Height + sf.Width) / 2;
            rightPos = Constants.CARD_DISTANCE + playerWest.Cards[0].Width + Constants.PLAYER_NAME_DISTANCE;
            e.Graphics.DrawString(playerWest.Name.Trim(), font, brush, leftPos, rightPos);

            //
            sf = e.Graphics.MeasureString(playerEast.Name.Trim(), font);
            rightPos = playerEast.Cards[0].Left - Constants.PLAYER_NAME_DISTANCE - (int)sf.Height;
            e.Graphics.DrawString(playerEast.Name.Trim(), font, brush, leftPos, rightPos);

        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            if ((Width - this.MinimumSize.Width) != 0)
                scaleWidth = Width / (float)MinimumSize.Width;

            if ((Height - this.MinimumSize.Height) != 0)
                scaleHeight = Height / (float)MinimumSize.Height;

            ReScaleCards();
        }


        private void btnShowHide_Click(object sender, EventArgs e)
        {
            foreach (Player player in game.Players)
            {
                foreach (Card card in player.Cards)
                {
                    card.Visible = !card.Visible;
                }

            }
        }

        #region ReScaleCards
        void ReScaleCards()
        {
            if (game != null)
            {
                game.ScaleCards(scaleWidth);
                TileCards();
            }
        }
        #endregion

        private void btnShuffle_Click(object sender, EventArgs e)
        {
        }



    }
}